# -*- coding: UTF-8 -*-

# Copyright (C) 2009 Daniele Favara.
# Written by Daniele Favara <daniele@zeroisp.com>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from tgext.zeroisp import model

from tw.api import Widget, WidgetsList, JSLink,CSSLink, CSSSource, JSSource, js_function
from tw.forms import *
from tw.forms.validators import *

__all__ = ['pack', 'datatables', 'tabs', 'Tabs', 'autocomplete','Pack', 'FieldForm', 'PostAddForm', 'PostContentAddForm',
           'AddressForm', 'GmapSearchForm', 'Gmap', 'LatLngForm', 'Fileupload','PasswordEditForm',
           'Datatables', 'DetailsForm', 'PostEditContentForm', 'PostEditForm']

multifarce_min_js = JSLink(modname=__name__,
                   filename='public/js/multifarce.min.js')

wymeditor_js = JSLink(modname=__name__,
                    filename='public/wymeditor/jquery.wymeditor.pack.js')


gmap_js = JSLink(modname=__name__,
                   filename='public/js/zeroisp.map.js',
                   )

jquery_autocomplete_js = JSLink(modname=__name__,
                   filename='public/js/jquery.autocomplete.js',
                   )

autocomplete_js = JSLink(modname=__name__,
                   filename='public/js/zeroisp.autocomplete.js',
                   )

jquery_blockui_js = JSLink(modname=__name__,
                    filename='public/js/multiple-file-upload/jquery.blockUI.js')

jquery_multifile_js = JSLink(modname=__name__,
                    filename='public/js/multiple-file-upload/jquery.MultiFile.js',
                    javascript=[jquery_blockui_js])

jquery_datatables_js = JSLink(modname=__name__,
                    filename='public/js/datatables/media/js/jquery.dataTables.min.js')

jquery_tabs_js = JSLink(modname=__name__,
                    filename='public/js/jquery.tabs/jquery.tabs.pack.js')


style_min_css = CSSLink(modname=__name__,
                           filename='public/css/style.min.css',
                           media='screen, projection, print')

datatables_css = CSSLink(modname=__name__,
                           filename='public/js/datatables/media/css/demo_table_jui.css',
                           media='screen, projection, print')

jquery_tabs_css = CSSLink(modname=__name__,
                           filename='public/js/jquery.tabs/jquery.tabs.css',
                           media='screen, projection, print')

class Pack(Widget):
    css = [style_min_css]
    javascript = [multifarce_min_js]

pack = Pack()

class Datatables(Widget):
    css = [datatables_css]
    javascript = [multifarce_min_js, jquery_datatables_js]

datatables = Datatables()

class Tabs(Widget):
    css = [style_min_css, jquery_tabs_css]
    javascript = [multifarce_min_js, jquery_tabs_js]

tabs = Tabs()

class Autocomplete(Widget):
    javascript = [jquery_datatables_js]

autocomplete = Autocomplete()

from tw.api import Widget, JSLink, CSSLink, js_function, js_callback

from tw.jquery.base import *
from tw.jquery.direction import *
from tw.forms import FormField


class FieldForm(TableForm):
    #action = "/toolbox/category/insert_field"
    submit_text = 'Save'
    class fields(WidgetsList):
        team_alias = HiddenField()
        category_id = HiddenField()
        fieldkey_name = InputField(css_class="text")
        fieldkey_label = InputField(css_class="text")
        fieldkey_type = InputField(css_class="text")
        fieldkey_order = InputField(css_class="text")
        fieldkey_value = InputField(css_class="text")

class PostAddForm(TableForm):
    submit_text = 'Save'
    javascript = [wymeditor_js]
    class fields(WidgetsList):
        team_alias = HiddenField()
        post_category_id = HiddenField()
        post_parent = HiddenField()
        post_title = InputField(css_class="title")
        post_description = TextArea(attrs=dict(rows=3, cols=80))
        submit = SubmitButton(css_class="wymupdate", value='Save')

class PostContentAddForm(TableForm):
    submit_text = 'Save'
    javascript = [wymeditor_js]
    class fields(WidgetsList):
        team_alias = HiddenField()
        post_content = TextArea(css_class="wymeditor",
                                suppress_label=True,
                                attrs=dict(rows=300, cols=80))
        submit = SubmitButton(css_class="wymupdate", value='Save')

class AddressForm(TableForm):
    submit_text = 'Save'

    class fields(WidgetsList):
        country_name_code = InputField(css_class="text")
        country_name = InputField(css_class="text")
        administrative_area_name = InputField(css_class="text")
        sub_administrative_area_name = InputField(css_class="text")
        locality_name = InputField(css_class="text")
        thoroughfare_name = InputField(css_class="text")
        postal_code_number = InputField(css_class="text")
        dependent_locality_name = InputField(css_class="text")
        google_lat = InputField(type="hidden", suppress_label=True)
        google_address = InputField(type="hidden", suppress_label=True)
        google_lng = InputField(type="hidden", suppress_label=True)
        accuracy = InputField(type="hidden", suppress_label=True)

class LatLngForm(ListForm):
    submit_text = 'Go'

    class fields(WidgetsList):
        google_lat = InputField(type="hidden", suppress_label=True)
        google_address = InputField(type="hidden", suppress_label=True)
        google_lng = InputField(type="hidden", suppress_label=True)
        accuracy = InputField(type="hidden", suppress_label=True)

class GmapSearchForm(FieldSet,ListForm):
    action='#'
    attrs={'onsubmit':'showAddress(this.search_address.value); return false'}
    legend = "Search Address Form"
    template = "tgext.zeroisp.templates.gmapsearch_form"
    javascript=[autocomplete_js]
    class fields(WidgetsList):
        search_address = InputField(css_class="title ac_input", attrs=dict(style="width: 95%;"))#SearchAddressCompleteField()
        submit = SubmitButton(attrs=dict(style="display: none;"))
        spacer = Spacer(suppress_label=True)

class Gmap(FormField):

    label_text = ''
    template = "tgext.zeroisp.templates.gmap"
    id = 'address'
    javascript = [gmap_js]

class Fileupload(Widget):
    javascript = [jquery_multifile_js]


class PasswordEditForm(TableForm):
    submit_text = 'Save'
    class fields(WidgetsList):
        password = PasswordField()
        verify   = PasswordField()

class DetailsForm(TableForm):
    def __init__(self, detail_id, id=None, parent=None, children=[], **kw):
        self.detail_id = detail_id
        setattr(self,'children',self.get_fields())
        super(TableForm, self).__init__(id, parent, children, **kw)

    def get_fields(self):
        ret = []
        fields_lst = model.DBSession.query(model.FieldKey).filter_by(
                                                fieldkey_detail_id=self.detail_id)
        fields_lst= fields_lst.order_by(model.FieldKey.fieldkey_order).all()

        for field in fields_lst:
            print field.fieldkey_type, field.fieldkey_name
            if field.fieldkey_type == 'select':
                options = []
                values = field.fieldkey_value.strip().split(',')
                ret.append(SingleSelectField(id=field.fieldkey_name,
                                             options=values,
                                             css_class="select"))
            if field.fieldkey_type == 'text':
                ret.append(InputField(id=field.fieldkey_name,
                                      css_class="text",
                                      help_text="help here"))
        return ret
    submit_text = 'Save'

class PostEditContentForm(TableForm):
    submit_text = 'Save'
    javascript = [wymeditor_js]
    class fields(WidgetsList):
        post_id = HiddenField('post_id')
        team_alias = HiddenField()
        post_content = TextArea(css_class="wymeditor",
                                suppress_label=True,
                                attrs=dict(rows=300, cols=80))
        submit = SubmitButton(css_class="wymupdate", value='Save')

class PostEditForm(PostAddForm):
    class fields(WidgetsList):
        post_id = HiddenField('post_id')
        team_alias = HiddenField()
        post_category_id = HiddenField()
        post_parent = HiddenField()
        post_title = InputField(css_class="title")
        post_description = TextArea(attrs=dict(rows=3, cols=80))
        submit = SubmitButton(css_class="wymupdate", value='Save')